package 栈;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class _144_二叉树的前中后序遍历 {

    public static void main(String[] args) {
//        System.out.println(preorderTraversal(new TreeNode(1, null, new TreeNode(2, new TreeNode(3), null))));
        System.out.println(preorderTraversal(new TreeNode(3, new TreeNode(1), new TreeNode(2, null, null))));
        System.out.println(inorderTraversal(new TreeNode(3, new TreeNode(1), new TreeNode(2, null, null))));
        System.out.println(postorderTraversal(new TreeNode(3, new TreeNode(1), new TreeNode(2, null, null))));
    }
    //递归
//    static List<Integer> result = new ArrayList();
//    public static List<Integer> preorderTraversal(TreeNode root) {
//        if (root == null) return new ArrayList<>();
//
//        result.add(root.val);
//        preorderTraversal(root.left);
//        preorderTraversal(root.right);
//        return result;
//    }

    //    public static List<Integer> preorderTraversal(TreeNode root) {
//        return preorderTraversal(new ArrayList<>(), root);
//    }
//
//    public static List<Integer> preorderTraversal(List<Integer> list, TreeNode root) {
//        if (root == null) return new ArrayList<>();
//        list.add(root.val);
//        preorderTraversal(list, root.left);
//        preorderTraversal(list, root.right);
//        return list;
//    }
    //迭代  根左右
    public static List<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        while (!stack.isEmpty() || node != null) {
            while (node != null) {
                res.add(node.val);
                stack.push(node);
                node = node.left;
            }

            node = stack.pop();
            node = node.right;
        }
        return res;
    }

    //左根右
    public static List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        while (!stack.isEmpty() || node != null) {
            while (node != null) {
                stack.push(node);
                node = node.left;
            }

            node = stack.pop();
            res.add(node.val);
            node = node.right;
        }
        return res;
    }

    //左右根
    public static List<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        TreeNode prev = null;
        while (!stack.isEmpty() || node != null) {
            while (node != null) {
                stack.push(node);
                node = node.left;
            }

            node = stack.pop();
            if (node.right == null || node.right == prev) {
                res.add(node.val);
                prev = node;
                node = null;
            } else {
                stack.push(node);
                node = node.right;
            }
        }
        return res;
    }

}
